package pers.sports.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import pers.sports.dao.ProjectDao;
import pers.sports.entity.Project;
import pers.sports.service.ProjectService;
import pers.sports.util.DateUtil;
import pers.sports.vo.ProjectSexNum;

import java.util.List;

@Service
@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
public class ProjectServiceImpl implements ProjectService {

    private final ProjectDao projectDao;

    @Autowired
    public ProjectServiceImpl(ProjectDao projectDao) {
        this.projectDao = projectDao;
    }


    @Override
    @Transactional(readOnly = true)
    public List<Project> findAllProject() {
        List<Project> list = projectDao.findAllProject();
        for (int i = 0; i < list.size(); i++) {
            list.get(i).setProjectStart(DateUtil.stampToDate(list.get(i).getProjectStart()));
            list.get(i).setProjectEnd(DateUtil.stampToDate(list.get(i).getProjectEnd()));
        }
        return list;
    }

    @Override
    @Transactional(readOnly = true)
    public ProjectSexNum findProjectNumById(String projectId) {
        return projectDao.findProjectNumById(projectId);
    }

    @Override
    public Integer insertApplyInfo(String stuId, String projectId) {
        return projectDao.insertApplyInfo(stuId, projectId);

    }

    @Override
    public Integer findAlreadySignedUp(String stuId, String projectId) {
        return projectDao.findAlreadySignedUp(stuId, projectId);
    }

    @Override
    public List<Project> findAll() {
        return projectDao.findAll();
    }
}
